
#include <gtk/gtk.h>
  enum {
        LIST_ITEM = 0,
        N_COLUMNS
    };
void initListView(GtkWidget *list)
{
	GtkListStore *store;
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *col;
	
	store = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING);
	renderer = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes("List Items", renderer, "text", LIST_ITEM, NULL);

	gtk_tree_view_append_column(GTK_TREE_VIEW(list),col);
	gtk_tree_view_set_model(GTK_TREE_VIEW(list),GTK_TREE_MODEL(store));
	g_object_unref(store);
}

void on_changed(GtkWidget * widget, gpointer label)
{
    GtkTreeIter iter;
    GtkTreeModel *model;
    char *value;
    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
        gtk_tree_model_get(model, &iter, LIST_ITEM, &value, -1);
        gtk_label_set_text(GTK_LABEL(label), value);
        g_free(value);
    }
}

void add2List(GtkWidget *list, gchar *data)
{
	GtkListStore *store;
	GtkTreeIter iter;
	store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
	gtk_list_store_append(store,&iter);
	gtk_list_store_set(store, &iter, LIST_ITEM,data,-1);
}
int main(int argc, char *argv[])
{
	GtkWidget *win;
	GtkWidget *box;
	GtkWidget *view;
	GtkWidget *label;
	GtkTreeSelection *selection;
	gtk_init(&argc, &argv);
	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(win), GTK_WIN_POS_CENTER);
	gtk_container_set_border_width(GTK_CONTAINER(win), 10);
	gtk_widget_set_size_request(win, 270, 250);
	gtk_window_set_title(GTK_WINDOW(win), "List View");

	view = gtk_tree_view_new();
	label = gtk_label_new("");
	box = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(box),view,TRUE,TRUE,5);
	gtk_box_pack_start(GTK_BOX(box),label,TRUE,TRUE,5);
	gtk_container_add(GTK_CONTAINER(win), box);

	initListView(view);
	add2List(view,"hello1");
	add2List(view,"hello2");
	add2List(view,"hello3");

	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
	g_signal_connect(selection, "changed",G_CALLBACK(on_changed), label);
	g_signal_connect(G_OBJECT(win),"destroy",G_CALLBACK(gtk_main_quit),NULL);
	gtk_widget_show_all(win);
	gtk_main();
	return 0;
}
